package LeedCode_BinaryTree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-11-21
 * Time: 09:15
 */
public class Demo3 {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }
    public static TreeNode createTree(){
        TreeNode A = new TreeNode(4);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(7);
        TreeNode D = new TreeNode(1);
        TreeNode E = new TreeNode(3);
        TreeNode F = new TreeNode(6);
        TreeNode G = new TreeNode(8);
        TreeNode H = new TreeNode(9);
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = H;
        return A;
    }
    public static TreeNode invertTree(TreeNode root) {
        if(root != null){
            //两种位置选择
            TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;

            invertTree(root.left);
            //不能在中间位置
            /*TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;*/
            invertTree(root.right);

            /*TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;*/
        }
        return root;
    }
    public static void main(String[] args) {
        Demo3.TreeNode root = createTree();
        TreeNode ret = invertTree(root);

    }
}
